package com.chanceit.ssss.adapters;

import org.gl.utils.lg;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.chanceit.ssss.activity.PageViewHandlerI;
import com.chanceit.ssss.activity.R;
import com.chanceit.ssss.fragments.BaseFragment;
import com.chanceit.ssss.fragments.CarerInfoFragment;
import com.chanceit.ssss.fragments.CarerProjectUpdateFragment;

public class CarerPagerAdapter extends FragmentPagerAdapter {
	Activity context;
	// String tabs[] = new String[3];
	// String tabs[] = new String[2];
	String tabs[] = new String[1];
	Fragment[] fagments = new Fragment[tabs.length];
	private String TAG = "CarerPagerAdapter";

	public Fragment[] getFagments() {
		return fagments;
	}

	public void setFagments(Fragment[] fagments) {
		this.fagments = fagments;
	}

	public CarerPagerAdapter(FragmentManager fm, Activity context) {
		super(fm);
		this.context = context;
		tabs = new String[] { context.getString(R.string.tab_title_carer_info) };
		/*
		 * , context.getString(R.string.tab_title_carer_message)
		 */
		// tabs = new String[] {
		// context.getString(R.string.tab_title_carer_info),
		// context.getString(R.string.tab_title_carer_project)/*,
		// context.getString(R.string.tab_title_carer_message) */};
	};

	@Override
	public Fragment getItem(int position) {
		Fragment fragment = fagments[position];
		if (fragment == null) {
			switch (position) {
			case 1:
				fragment = new CarerProjectUpdateFragment();
				fagments[1] = fragment;
				break;
			case 0:
				fragment = new CarerInfoFragment();
				((CarerInfoFragment) fragment)
						.setViewPageHandler((PageViewHandlerI) context);
				fagments[0] = fragment;
				break;
			}
		}
		return fragment;

	}

	@Override
	public int getCount() {
		return tabs.length;
	}

	@Override
	public CharSequence getPageTitle(int position) {
		return tabs[position];
	}

	public void sendMessage(int postion, String stringExtra) {

		if (fagments[postion] != null
				&& fagments[postion] instanceof BaseFragment) {
			((BaseFragment) fagments[postion]).receiveMessage(stringExtra);
		} else {
			lg.e(TAG,
					"Exception,the fragment is null,or not BaseFragment ,so it can not receive message,");
		}

	}
}